var $certusUtils="";
var $$scope="";
function getUtils(CertusFormAjax,$scope) {
    CertusFormAjax.ajax("findQYList",{},function (response) {
        $scope.area = response.data;
    });

    CertusFormAjax.ajax("findComList",{},function (response) {
        var nhlTeams = [];
        var data = response.data;
        for (var i=0;i<data.length;i++){
            var arr={};
            arr.name=data[i].comName;
            nhlTeams.push(arr);
        }
        //自动补全
        var nhl = $.map(nhlTeams, function (obj) {
            return {
                value: obj.name,
                data: { category: obj.id }
            };
        });
        // Initialize autocomplete with local lookup:
        $('#cusName').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {

            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });
    });

    CertusFormAjax.ajax("findSpUserList",{},function (response) {
        var nhlTeams = [];
        var data = response.data;
        for (var i=0;i<data.length;i++){
            var arr={};
            arr.name=data[i].userName+"["+data[i].positonName+"]";
            arr.id = data[i].userId;
            nhlTeams.push(arr);
        }
        //自动补全
        var nhl = $.map(nhlTeams, function (obj) {
            return {
                value: obj.name,
                data: { category: obj.id }
            };
        });
        // Initialize autocomplete with local lookup:
        $('#autocomplete').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#searchParam").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });
    });
    $certusUtils= CertusFormAjax;
    $$scope= $scope;
}

//添加客户公司
function addCom() {
    if (!certusValidateNotnull()){
        return;
    }
    var arr =$("form").serializeArray();
    var data = {};
    for (var i=0;i<arr.length;i++){
        data[arr[i].name] = arr[i].value;
    }
    $certusUtils.ajax('insertComp',{"comArr":JSON.stringify(data),"id":0},function (result) {
        if (result.data==true){
            layer.msg("添加成功");
            location.href="#/toCustomer";
        }
    })
}
//查看和修改客户公司
function findCusInfo(CertusFormAjax,$scope,$routeParams) {

    CertusFormAjax.ajax("findQYList",{},function (response) {
        $scope.edarea = response.data;
    });

    CertusFormAjax.ajax("findSpUserList",{},function (response) {
        var nhlTeams = [];
        var data = response.data;
        for (var i=0;i<data.length;i++){
            var arr={};
            arr.name=data[i].userName+"["+data[i].positonName+"]";
            arr.id = data[i].userId;
            nhlTeams.push(arr);
        }
        //自动补全
        var nhl = $.map(nhlTeams, function (obj) {
            return {
                value: obj.name,
                data: { category: obj.id }
            };
        });
        // Initialize autocomplete with local lookup:
        $('#autocomplete').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#searchParam").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });
    });

    CertusFormAjax.ajax('findProComInfo',{"id":$routeParams.id},function (result) {
        var data = result.data;
        $scope.industry = data.industry;
        $scope.area = data.area;
        $scope.cusName =data.com_name;
        $scope.cusShortName = data.com_short_name;
        $scope.userName = data.user_name+"("+data.user_id+")"+"["+data.positon_name+"]";
        $scope.comType = data.com_type;
        $scope.telephone = data.telephone;
        $scope.address = data.address;
        $scope.backDesc = data.com_back_desc;
        $scope.userId = data.user_id;
    });

    $(".update").on("click",function () {
        var arr =$("form").serializeArray();
        var data = {};
        for (var i=0;i<arr.length;i++){
            data[arr[i].name] = arr[i].value;
        }
        CertusFormAjax.ajax('insertComp',{"comArr":JSON.stringify(data),"id":$routeParams.id},function (result) {
            if (result.data==true){
                layer.msg("修改成功");
                location.href="#/toCustomer";
            }
        })
    })
}

var $$certusUtils="";
var $$routeParams ="";
//查看联系人信息
function findLxrInfo(CertusFormAjax,$routeParams){
    $$routeParams=$routeParams;
    $$certusUtils=CertusFormAjax;
    CertusFormAjax.ajax('isCanDel',{},function (result) {
        var isdel = false;
        if (result.data=="true"){
            isdel=true;
        }
        var telColumns = [{
            id: 'cusName',
            title: '联系人名称',
            type: 'string',
            width: 90,
            columnClass: 'text-center'
        },
            {
                id: 'telePhone',
                title: '联系方式',
                type: 'string',
                width: 110,
                columnClass: 'text-center'
            },
            {
                id: 'email',
                title: '邮件地址',
                type: 'string',
                width: 200,
                columnClass: 'text-center'
            },
            {
                id: 'cusDesc',
                title: '注释',
                type: 'string',
                columnClass: 'text-left'
            },
            {
                id: 'operation',
                title: '操作',
                type: 'string',
                width: 75,
                columnClass: 'text-center',
                resolution: function (value, record, column, grid, dataNo, columnNo) {
                    var content = '';
                    content += '<button class="btn btn-xs btn-info" onclick="updateCus(' + record.id + ');"><i class="fa fa-edit"></i>  编辑</button>';
                    content += '  ';
                    if (isdel){
                        content += '<button class="btn btn-xs btn-danger" onclick="delCus(' + record.id + ');"><i class="fa fa-trash-o"></i>  删除</button>';
                    }
                    return content;
                }
            }
        ];
        $("#telContainer").empty();
        $("#telToolBarContainer").empty();
        CertusFormAjax.ajax('findCusList',{"comid":$routeParams.id},function (result) {
            var telOption = {
                lang: 'zh-cn',
                ajaxLoad: false,
                extraWidth: 20,
                // loadURL:'url',
                exportFileName: '联系人管理',
                datas: result.data,
                columns: telColumns,
                gridContainer: 'telContainer',
                toolbarContainer: 'telToolBarContainer',
                tools: '',
                pageSize: 10,
                pageSizeLimit: [10, 15, 20]
            };
            var telGrid = $.fn.dlshouwen.grid.init(telOption);
            telGrid.load();
        });

    })
}

function toAddCus(CertusFormAjax,$scope,$routeParams) {
    $(".add").on("click",function () {
        var name = $("#cusName").val();
        var phone = $("#phone").val();
        var email = $("#email").val();
        var desc = $("#backDesc").val();
        if (name == "" || name == null || name == undefined){
            layer.msg("联系人名称不能为空！");
            return;
        }
        if (phone == "" || phone == null || phone == undefined){
            layer.msg("联系方式不能为空！");
            return;
        }
        if (email == "" || email == null || email == undefined){
            layer.msg("邮件地址不能为空！");
            return;
        }
        var obj={};
        obj.name=name;
        obj.phone=phone;
        obj.email=email;
        obj.desc= desc;
        CertusFormAjax.ajax("insertCust",{"json":JSON.stringify(obj),"comid":$routeParams.id}, function(result){
        var data = result.data;
        if (data==true){
            layer.msg("添加成功");
            // $("#cusName").val("");
            // $("#phone").val("");
            // $("#email").val("");
            // $("#backDesc").val("");
            window.history.back();
        }else {
            layer.msg("添加失败");
        }
    })
    });
}

function delCus(id) {
    layer.confirm('是否确认删除？', {
        btn : [ '确定', '取消' ]//按钮
    }, function(index) {
        $$certusUtils.ajax('delCus',{"id":id},function (result) {
            if (result.data==true){
                layer.msg("删除成功");
                findLxrInfo($$certusUtils,$$routeParams);
            }
        })
    });
}

function updateCus(id) {
    window.location.href = "#/toUpdateCus/"+id;
}
function toUpdateCus(CertusFormAjax,$scope,$routeParams) {
    CertusFormAjax.ajax('findCusInfo',{"id":$routeParams.id},function (result) {
        var data = result.data;
        $scope.cusName= data.cusName;
        $scope.phone = data.telePhone;
        $scope.email = data.email;
        $scope.backDesc = data.cusDesc;
    });
    $(".updateCus").on("click",function () {
        var name = $("#cusName").val();
        var phone = $("#phone").val();
        var email = $("#email").val();
        var desc = $("#backDesc").val();
        if (name == "" || name == null || name == undefined){
            layer.msg("联系人名称不能为空！");
            return;
        }
        if (phone == "" || phone == null || phone == undefined){
            layer.msg("联系方式不能为空！");
            return;
        }
        if (email == "" || email == null || email == undefined){
            layer.msg("邮件地址不能为空！");
            return;
        }
        var obj={};
        obj.name=name;
        obj.phone=phone;
        obj.email=email;
        obj.desc= desc;
        CertusFormAjax.ajax("updateCust",{"json":JSON.stringify(obj),"id":$routeParams.id}, function(result) {
            var data = result.data;
            if (data == true) {
                layer.msg("修改成功");
                window.history.back();
            } else {
                layer.msg("添加失败");
            }
        })
    })
}